package com.wits.qso.web.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import com.wits.platform.common.constants.WebConstants;
import com.wits.platform.common.utils.PageUtil;
import com.wits.platform.common.utils.StringUtil;
import com.wits.platform.web.controller.BaseController;
import com.wits.platform.web.mvc.PageParam;
import com.wits.qso.entity.BookCase;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.service.BookCaseService;
import com.wits.qso.service.MaterialdService;
import com.wits.qso.service.TeacherService;

@Controller
@RequestMapping(value = "qso/bookcase")
public class BookCaseController extends BaseController {
	
	@Autowired
	private BookCaseService bookCaseService;
	@Autowired
	private MaterialdService materialdService;
	@Autowired
	private TeacherService teacherService;
	
	@RequestMapping(value="/toList")
	public String toList(){
		return "qso/bookcase/list";
	}
	
	/**
	 * 加载书柜列表信息
	 * @param model
	 * @param page
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public String list(Model model,@ModelAttribute("page") PageParam page, HttpServletRequest req){
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(req, "search_");
		Page<BookCase> listPage = bookCaseService.findPage(BookCase.class, searchParams, page);
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return PageUtil.toAjaxPageResult(listPage.getTotalElements(), listPage.getContent());
	}
	
	/**
	 * 列表上按钮的触发事件处理
	 * @param model
	 * @param viewType
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/view/{viewType}")
	public String toView(Model model,@PathVariable String viewType,@RequestParam String id){
		
		BookCase bc=bookCaseService.getOne(id);
		MaterialD materialD=materialdService.getOne(bc.getMatId().getId());
		model.addAttribute(WebConstants.COMMAND,materialD);
		
		model.addAttribute("viewType", viewType);
		return "qso/bookcase/form";
	}
	
	/**
	 * 新增、修改我的书柜信息
	 * @param model
	 * @param command
	 * @param viewType
	 * @param result
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(String ids){
		try {
			BookCase bc=null;
			Date currentDate=new Date();
			for(String id : Arrays.asList( StringUtil.split( ids, "," ) )) {
				bc=new BookCase();
				bc.setDateCre(currentDate);
				bc.setDateUpd(currentDate);
				bc.setUserUpd(getLocaleString());
				bc.setUserCre(getLocaleString());
				bc.setTerId(teacherService.findByUserId(getCurrentUserId()));
				bc.setMatId(materialdService.getOne(id));
				bookCaseService.save(bc);
			}
			return ajaxDoneSuccess(this.getText("save.success"));
		} catch (Exception e) {
			return ajaxDoneError(this.getText("save.failed"), e);
		}
	}
	
	/**
	 * 删除书柜信息
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(String ids){
		try {
			List<String> list=Arrays.asList(StringUtil.split(ids, ","));
			bookCaseService.deleteInBatch(list);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		
		return ajaxDoneSuccess(this.getText("delete.success"));
	}
	
	/**
	 * 唯一键检查
	 * @param clsId
	 * @param oldClsId
	 * @return
	 */
	@RequestMapping(value="/checkUniqueId")
	@ResponseBody
	public boolean checkUniqueId(@RequestParam("clsId") String clsId,@RequestParam("oldClsId") String oldClsId){
		if(clsId.equals(oldClsId)){
			return true;
		}
		BookCase obj=bookCaseService.getOne(clsId);
		return obj == null;
	}
}
